package com.k.demo.controller;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.k.demo.entity.CustomerUser;
import com.k.demo.entity.RedBag;
import com.k.demo.entity.RedBagRecord;
import com.k.demo.service.RedBagRecordService;
import com.k.demo.service.RedBagService;
import com.k.demo.service.VipService;

@RestController
@RequestMapping("/redbagrecord")
public class RedBagRecordController {
	
	@Autowired
	private RedBagRecordService redBagRecordService;
	
	@Autowired
	private RedBagService redBagService;
	
	@Autowired
	private VipService vipService;
	
	@RequestMapping("/getall")
	public Object getAll(HttpSession session) throws ParseException {
		Map<String,Object> map = new HashMap<String, Object>();
		CustomerUser login_user=(CustomerUser)session.getAttribute("loginUser");
		List<RedBagRecord> redbagrecordlist = redBagRecordService.getByState(61, login_user.getUser_id());
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 获取当前时间    
        Date date = new Date();
        String nowtime = df.format(date);
        // System.out.println(nowtime);
          //
        Date d1 = df.parse(nowtime);
        
		for(RedBagRecord red : redbagrecordlist) {
			Date d2 = df.parse(red.getCreate_date());
			long diff = d1.getTime() - d2.getTime();// 这样得到的差值是微秒级别
	        long days = diff / (1000 * 60 * 60 * 24);//天
			if(days>red.getValid_time()) {//如果过期了直接更新
				redBagRecordService.changeState(63, login_user.getUser_id(), red.getRed_bag_id());
			}
		}
		//先更新了再拿
		map.put("redbaglist", redBagRecordService.getAll(login_user.getUser_id()));
		
		return map;
	}
	
	//1是未使用  2是已使用  3是已过期  到时前端要对应好传过来
	@RequestMapping("/getByState")
	public Object getByState(@RequestParam int redbag_state,HttpSession session) throws ParseException {
		
		Map<String,Object> map = new HashMap<String, Object>();
		
		CustomerUser login_user=(CustomerUser)session.getAttribute("loginUser");
		
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 获取当前时间    
        Date date = new Date();
        String nowtime = df.format(date);
        // System.out.println(nowtime);
          //
        Date d1 = df.parse(nowtime);
        
        List<RedBagRecord> redbagrecordlist = redBagRecordService.getAll(login_user.getUser_id());
		for(RedBagRecord red  : redbagrecordlist) {
			Date d2 = df.parse(red.getCreate_date());
			long diff = d1.getTime() - d2.getTime();// 这样得到的差值是微秒级别
	        long days = diff / (1000 * 60 * 60 * 24);//天
			if(days>red.getValid_time()) {//如果过期了直接更新
				redBagRecordService.changeState(63, login_user.getUser_id(), red.getRed_bag_id());
			}
		}
		
		map.put("redbaglist", redBagRecordService.getByState(redbag_state, login_user.getUser_id()));
		
		return map;
		
	}
	@RequestMapping("/use")
	public Object use(@RequestParam int red_bag_id,HttpSession session) {
		Map<String,Object> map = new HashMap<String, Object>();
		
		CustomerUser login_user=(CustomerUser)session.getAttribute("loginUser");
		
		//修改为已使用
		int count=redBagRecordService.changeState(62, login_user.getUser_id(),red_bag_id);
		
		map.put("usenum", count);
		
		return map;
	}
	
	//领取红包
	@RequestMapping("/gain")
	public Object gainRedBag(@RequestParam int red_bag_id,HttpSession session) {
		
		Map<String,Object> map = new HashMap<String, Object>();
		
		CustomerUser login_user=(CustomerUser)session.getAttribute("loginUser");
		//通过RedBagService查询对应红包  新建红包记录对象  给与相应的值
		RedBag redBag = redBagService.getRedBagById(red_bag_id).get(0);
		Integer nowUserVipLevel;
		
		//通过查询当前用户的VIP等级判断是否有领取的资格
		//如果没有领取资格 只能领取vip等级为一的红包
		if(vipService.getByUserId(login_user.getUser_id()) == null) {
			//当前用户不是会员只能领取无门槛红包
			if (redBag.getAvailable_level()==0) {
				DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		        Date date = new Date();
		        String nowtime = df.format(date);
				RedBagRecord redBagRecord = new RedBagRecord();
				redBagRecord.setRed_bag_id(redBag.getRed_bag_id());
				redBagRecord.setUser_id(login_user.getUser_id());
				redBagRecord.setUser_true_name(login_user.getUser_true_name());
				redBagRecord.setCreate_date(nowtime);
				redBagRecord.setValid_time(redBag.getValid_time());
				redBagRecord.setRed_bag_state(61);
				
				//返回拿取结果(影响的行数)
				map.put("gainResult", redBagRecordService.addRedBagRecord(redBagRecord));
				//返回更新的可领取红包
				map.put("gainList", redBagService.getAvailableRedBag(login_user.getUser_id()));
				//返回更新的可用
				map.put("useList", redBagRecordService.getAll(login_user.getUser_id()));
				map.put("errorMsg", true);
				
				return map;
			} else {
				
				map.put("errorMsg", false);
				map.put("errorContent", "您不是vip，请充值");
				return map;
				
			}
		}else {
			//当前用户为会员则进行比较
			 nowUserVipLevel = vipService.getByUserId(login_user.getUser_id()).getVip_lv();
			 if(nowUserVipLevel >= redBag.getAvailable_level()) {
					//可以领取
					// 获取当前时间
					DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			        Date date = new Date();
			        String nowtime = df.format(date);
			        
					RedBagRecord redBagRecord = new RedBagRecord();
					redBagRecord.setRed_bag_id(redBag.getRed_bag_id());
					redBagRecord.setUser_id(login_user.getUser_id());
					redBagRecord.setUser_true_name(login_user.getUser_true_name());
					redBagRecord.setCreate_date(nowtime);
					redBagRecord.setValid_time(redBag.getValid_time());
					redBagRecord.setRed_bag_state(61);
					
					map.put("gainResult", redBagRecordService.addRedBagRecord(redBagRecord));
					//返回更新的可领取红包
					map.put("gainList", redBagService.getAvailableRedBag(login_user.getUser_id()));
					//返回更新的可用
					map.put("useList", redBagRecordService.getAll(login_user.getUser_id()));
					map.put("errorMsg", true);
					
					return map;
				}else {
					map.put("errorMsg", false);
					map.put("errorContent", "当前用户vip等级不够，请充值");
					return map;
				}
		}
		
		
		

		
	}
	//根据传入的红包记录对象进行查找
	@RequestMapping("/searchRecordWithPage")
	public Object searchWithPage(@RequestBody RedBagRecord redBagRecord, @RequestParam(value = "pageNum") Integer pageNum, @RequestParam(value = "pageSize") Integer pageSize) {
		Map<String,Object> rmap = new HashMap<String, Object>();
		rmap.put("searchResultWithPage", redBagRecordService.searchResultWithPage(pageNum, pageSize, redBagRecord));		
		return rmap;
	}
	
	//红包过期
//	@RequestMapping("/expired")
//	public Object expired(@RequestParam int red_bag_id,HttpSession session) {
//		Map<String,Object> map = new HashMap<String, Object>();
//		CustomerUser login_user=(CustomerUser)session.getAttribute("loginUser");
//		
//		int count=redbagService.changeState(3, login_user.getUser_id(),red_bag_id);
//		
//		map.put("expirednum", count);
//		
//		return map;
//	}
	@RequestMapping("/all")
	public Object all() {
		
		Map<String,Object> rmap = new HashMap<String, Object>();
		
		return rmap.put("allList", redBagRecordService.getAllUserRecords());
	}

}
